package com.runa.monitor.platinterface.backwork.impl;

import com.alibaba.fastjson.JSONObject;
import com.runa.monitor.comm.dto.newComm.Archive;
import com.runa.monitor.comm.dto.newComm.AsynResult;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.persistence.util.SpringContextUtil;
import com.runa.monitor.platinterface.mqtt.MqttUtil;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.monitor.platinterface.platService.RepositoryService;
import com.runa.rccprotocol.dto.AffirmDeny;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class WriteArchiveTaskFunction implements TaskFunction<List<Integer>, Object, Void> {

    private final String task;

    private final String holdInfo;

    private final int priority;

    private final String netEquId;

    private List<Integer> indexes;

    private final RepositoryService repositoryService;

    public WriteArchiveTaskFunction(String task, String holdInfo, int priority, String netEquId) {
        this.task = task;
        this.holdInfo = holdInfo;
        this.priority = priority;
        this.netEquId = netEquId;
        this.repositoryService = SpringContextUtil.getBean(RepositoryService.class);
    }

    @Override
    public void setStatus(List<Integer> indexes) {
        this.indexes = indexes;
    }

    @Override
    public void doApply(CommStatus status, Object result) {
        log.info("WriteArchiveTaskFunction daApply {} {}", status, JSONObject.toJSONString(result));
        AsynResult asynResult = AsynResult.success();
        asynResult.setTask(task);
        asynResult.setHoldInfo(holdInfo);
        if (CommStatus.OK.equals(status)) {
            if (result instanceof AffirmDeny) {
                AffirmDeny affirmDeny = (AffirmDeny) result;
                for (Integer index : indexes) {
                    asynResult.setIdentity(repositoryService.getCommNodeIdByIndexAndNetId(netEquId, index));
                    if (!affirmDeny.getData()) {
                        asynResult.denySetting();
                    }
                    MqttUtil.send(Archive.TOPIC, asynResult);
                }
            }
        } else if (CommStatus.OffLine.equals(status)) {
            List<Integer> archives = (List<Integer>) result;
            for (Integer index : archives) {
                asynResult.offlineSetting();
                asynResult.setIdentity(repositoryService.getCommNodeIdByIndexAndNetId(netEquId, index));
                MqttUtil.send(Archive.TOPIC, asynResult);
            }
        } else if (CommStatus.TimeOut.equals(status)) {
            for (Integer index : indexes) {
                asynResult.timeoutSetting();
                asynResult.setIdentity(repositoryService.getCommNodeIdByIndexAndNetId(netEquId, index));
                MqttUtil.send(Archive.TOPIC, asynResult);
            }
        }
    }

    @Override
    public Void waitResult() {
        return null;
    }

    @Override
    public String getSerial() {
        return null;
    }

    @Override
    public int getPriority() {
        return priority;
    }
}
